package com.taichuan.keysupport.keyset;

import com.taichuan.keysupport.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by OISNY on 2016/8/18.
 */
public class T12KeySet extends KeySet {

    public static final int[] X_DRAWABLE = {R.drawable.t12key_back};

    public static final String[] ZERO_TEXT = {"0", "0", "0 =~`\""};

    public static final int[] J_DRAWABLE = {R.drawable.t12key_symbol_1,
            R.drawable.t12key_symbol_2, R.drawable.t12key_symbol_3};

    private String[] currentPanelArray;
    private int xIndex = 0;
    private int jIndex = 0;
    private int keyArrayIndex = 0;
    private int xDrawableId = X_DRAWABLE[0];
    private int jDrawableId = J_DRAWABLE[0];
    private String zeroText = ZERO_TEXT[0];

    public T12KeySet(OnKeySupportListener listener) {
        super(listener);
    }

    @Override
    public void performClickX() {
    }

    @Override
    public void performClickZero() {
        mOnKeySupportListener.updateEdit(ZERO_TEXT[0]);
    }

    @Override
    public void performClickJ() {
        if (jIndex < J_DRAWABLE.length - 1) {
            jIndex++;
        } else {
            jIndex = 0;
        }
        jDrawableId = J_DRAWABLE[jIndex];
        zeroText = ZERO_TEXT[jIndex];
        updatePanelView();
    }

    @Override
    public void updatePanelView() {
        if (mOnKeySupportListener != null) {
            mOnKeySupportListener.setXDrawable(xDrawableId, xIndex);
            mOnKeySupportListener.setZeroText(zeroText, jIndex);
            mOnKeySupportListener.setJDrawable(jDrawableId, jIndex);
            mOnKeySupportListener.upGridViewData(getGridViewData());
        }
    }

    @Override
    public void performKey(int index, boolean isSameKey) {
        if (mOnKeySupportListener != null) {
            if (jIndex == 0) {
                mOnKeySupportListener.updateEdit(KEYBOARD_MATH[index - 1]);
            } else if (jIndex == 1) {
                if (isSameKey) {
                    if (keyArrayIndex < KEYBOARD_INS[index - 1].length - 1) {
                        keyArrayIndex++;
                    } else {
                        keyArrayIndex = 0;
                    }
                } else {
                    keyArrayIndex = 0;
                }
                mOnKeySupportListener.updateEdit(KEYBOARD_INS[index - 1][keyArrayIndex]);
            } else if (jIndex == 2) {
                if (isSameKey) {
                    if (keyArrayIndex < KEYBOARD_SYMBOL_INS[index - 1].length - 1) {
                        keyArrayIndex++;
                    } else {
                        keyArrayIndex = 0;
                    }
                } else {
                    keyArrayIndex = 0;
                }
                mOnKeySupportListener.updateEdit(KEYBOARD_SYMBOL_INS[index - 1][keyArrayIndex]);
            }
        }
    }

    @Override
    public List<String> getGridViewData() {
        if (jIndex == 0) {
            currentPanelArray = KEYBOARD_MATH;
        } else if (jIndex == 1) {
            currentPanelArray = KEYBOARD_EN;
        } else {
            currentPanelArray = KEYBOARD_SYMBOL;
        }
        List<String> lists = new ArrayList<String>();
        Collections.addAll(lists, currentPanelArray);
        return lists;
    }

}
